package com.work.yshop.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

import com.work.yshop.R;
import com.work.yshop.databinding.FragmentSellerProfileBinding;
import com.work.yshop.ui.activity.LoginActivity;
import com.work.yshop.ui.activity.MainActivity;
import com.work.yshop.ui.activity.OrderManageActivity;
import com.work.yshop.ui.activity.ProductListActivity;
import com.work.yshop.ui.activity.SettingsActivity;
import com.work.yshop.utils.UserSession;

/**
 * 商家端个人中心Fragment
 */
public class SellerProfileFragment extends Fragment {

    private FragmentSellerProfileBinding binding;

    public static SellerProfileFragment newInstance() {
        return new SellerProfileFragment();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentSellerProfileBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        
        // 初始化数据
        initData();
        // 设置监听器
        setupListeners();
    }

    /**
     * 初始化数据
     */
    private void initData() {
        // 设置商家信息
        binding.tvUsername.setText(UserSession.getInstance().getUsername());
        binding.tvUserRole.setText("商家");
        
        // 设置商家统计数据
        binding.tvProductCount.setText("12");
        binding.tvOrderCount.setText("156");
        binding.tvSalesAmount.setText("¥ 12,580.00");
    }

    /**
     * 设置监听器
     */
    private void setupListeners() {
        // 商品管理
        binding.menuProductManage.setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), ProductListActivity.class);
            startActivity(intent);
        });
        
        // 订单管理
        binding.menuOrderManage.setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), OrderManageActivity.class);
            startActivity(intent);
        });
        
        // 店铺数据
        binding.menuShopData.setOnClickListener(v -> 
                showToast("店铺数据功能即将上线"));
        
        // 店铺设置
        binding.menuShopSettings.setOnClickListener(v -> 
                showToast("店铺设置功能即将上线"));
        
        // 营销中心
        binding.menuMarketingCenter.setOnClickListener(v -> 
                showToast("营销中心功能即将上线"));
        
        // 客服中心
        binding.menuCustomerService.setOnClickListener(v -> 
                showToast("客服中心功能即将上线"));
        
        // 切换到买家
        binding.menuSwitchToBuyer.setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), MainActivity.class);
            startActivity(intent);
            if (getActivity() != null) {
                getActivity().finish();
            }
        });
        
        // 设置
        binding.menuSettings.setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), SettingsActivity.class);
            startActivity(intent);
        });
        
        // 退出登录
        binding.menuLogout.setOnClickListener(v -> 
                showLogoutConfirmDialog());
    }
    
    /**
     * 显示Toast提示
     */
    private void showToast(String message) {
        Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
    }
    
    /**
     * 显示退出登录确认对话框
     */
    private void showLogoutConfirmDialog() {
        new AlertDialog.Builder(requireContext())
                .setTitle("退出登录")
                .setMessage("确定要退出登录吗？")
                .setPositiveButton("确定", (dialog, which) -> logout())
                .setNegativeButton("取消", null)
                .show();
    }
    
    /**
     * 退出登录
     */
    private void logout() {
        // 清除用户会话
        UserSession.getInstance().clear();
        
        // 跳转到登录页面
        Intent intent = new Intent(getActivity(), LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
} 